<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="core.*,util.*" %>
<%@ page import="java.util.*" %>
<%@ include file="/WEB-INF/include/checkAdmin.jsp" %>
<!DOCTYPE html>
<html>
<head>
<%@ include file="/WEB-INF/include/header.jsp" %>
<style>
    div.form-inline { margin-bottom: 5px; }
    select[name=sz] { width: 60px; }
    select[name=od] { width: 100px; }
    select[name=ss] { width: 120px; margin-left: 70px; }
    select[name=ct] { width: 180px; }
    button { margin-right: 60px; }
    thead { background-color: #eee; }
    table.table th:first-child { width: 20px; }
    tbody tr:hover { background-color: #FFC; cursor: pointer; }
</style>
<script>
$(function() {
    $("select[name=od], select[name=sz], select[name=ct]").change(function() {
        $("form").submit();
    })
    $("tbody tr").click(function() {
        location.href = "userEdit.jsp?id=" + $(this).attr("data-id") + "&" + location.search.replace("?","");
    })
    $("div.pagination a").click(function() {
        $("input[name=pg]").val($(this).attr("data-page"));
        $("form").submit();
    })
})
</script>
</head>
<%
    RequestParameter param = new RequestParameter(request);
    int currentPage = param.getInt("pg", 1);
    int pageSize = param.getInt("sz", 15);
    int order = param.getInt("od", 0);
    int srchType = param.getInt("ss", 0);
    String srchText = param.getString("st", "");
    int category = param.getInt("ct", 0);
    if (srchType == 0) srchText = "";

    ArrayList<User> list = UserDAO.selectPage(currentPage, pageSize, order, srchType, srchText, category);
    ArrayList<UserGroup> userGroupList = UserGroupDAO.selectAll();

    int recordCount = UserDAO.getRecordCount(srchType, srchText, category);
    int pageCount = (recordCount + pageSize - 1) / pageSize;
    int basePage = ((currentPage - 1) / 10) * 10;
%>
<body>
<%@ include file="/WEB-INF/include/menu.jsp" %>

<div class="container main">

    <h1>사용자 목록</h1>
    <hr />

    <form>
        <input type="hidden" name="pg" value="1" />

        <div class="form-inline">
            정렬:
            <select name="od">
                <option value="0" <%= order==0 ? "selected" : "" %>>ID</option>
                <option value="1" <%= order==1 ? "selected" : "" %>>로그인ID</option>
                <option value="2" <%= order==2 ? "selected" : "" %>>이름</option>
            </select>

            <select name="ss">
                <option value="0" <%= srchType==0 ? "selected" : "" %>>검색없음</option>
                <option value="1" <%= srchType==1 ? "selected" : "" %>>로그인ID</option>
                <option value="2" <%= srchType==2 ? "selected" : "" %>>이름</option>
                <option value="3" <%= srchType==3 ? "selected" : "" %>>이메일</option>
                <option value="4" <%= srchType==4 ? "selected" : "" %>>비활성상태</option>
            </select>

            <input type="text" name="st" value="<%= srchText %>" />
            <button type="submit" class="btn btn-small search">검색</button>

            사용자그룹:
            <select name="ct">
                <option value="0" <%= category == 0 ? "selected" : "" %>>전체</option>
                <% for (UserGroup group : userGroupList) { %>
                    <option value="<%=group.getId()%>" <%=group.getId() == category ? "selected" : ""%>>
                        <%= group.getName() %>
                    </option>
                <% } %>
            </select>
        </div>

        <table class="table table-condensed table-bordered">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>로그인ID</th>
                    <th>이름</th>
                    <th>이메일</th>
                    <th>활성</th>
                </tr>
            </thead>
            <tbody>
                <% for (User user : list) { %>
                    <tr data-id="<%=user.getId()%>">
                        <td><%=user.getId()%></td>
                        <td><%=user.getLoginId()%></td>
                        <td><%= user.getName() %></td>
                        <td><%= user.getEmail() %></td>
                        <td><%= user.isEnabled() %></td>
                    </tr>
                <% } %>
            </tbody>
        </table>

        <div class="controls pull-right form-inline">
            화면크기:
            <select name="sz">
                <option <%= pageSize==10 ? "selected" : "" %>>10</option>
                <option <%= pageSize==15 ? "selected" : "" %>>15</option>
                <option <%= pageSize==30 ? "selected" : "" %>>30</option>
                <option <%= pageSize==60 ? "selected" : "" %>>60</option>
                <option <%= pageSize==100 ? "selected" : "" %>>100</option>
            </select>
        </div>

        <div class="pagination pagination-small pagination-centered">
            <ul>
                <% if (basePage > 0) { %>
                    <li><a data-page="<%= basePage %>">Prev</a></li>
                <% } %>
                <% for (int i=1; i <= 10 && basePage + i <= pageCount; ++i) { %>
                    <li class='<%= basePage+i==currentPage ? "active" : "" %>'>
                        <a data-page="<%= basePage+i %>"><%= basePage + i %></a>
                    </li>
                <% } %>
                <% if (basePage + 11 <= pageCount) { %>
                    <li><a data-page="<%= basePage+11 %>">Next</a></li>
                <% } %>
            </ul>
        </div>

    </form>

</div>

<%@ include file="/WEB-INF/include/footer.jsp" %>
</body>
</html>
